package org.rsbot.client;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.rsbot.Application;
import org.rsbot.bot.Bot;
import org.rsbot.util.PreferenceData;
import org.rsbot.util.UIDData;

public class RandomAccessFile
{
  private UIDData uidData = null;
  private PreferenceData prefData = null;
  private java.io.RandomAccessFile raf = null;
  private Client client = null;

  private byte[] data = null;
  private int offset = 0;

  public RandomAccessFile(String paramString1, String paramString2) throws FileNotFoundException
  {
    if (!shouldOverride(paramString1, paramString2))
      this.raf = new java.io.RandomAccessFile(paramString1, paramString2);
  }

  public RandomAccessFile(File paramFile, String paramString) throws FileNotFoundException
  {
    if (!shouldOverride(paramFile.getName(), paramString))
      this.raf = new java.io.RandomAccessFile(paramFile, paramString);
  }

  private boolean shouldOverride(String paramString1, String paramString2) throws FileNotFoundException
  {
    if (paramString1.equals("random.dat"))
      this.uidData = new UIDData();
    else if (paramString1.endsWith("preferences.dat"))
      this.prefData = new PreferenceData(1);
    else if (paramString1.endsWith("preferences2.dat"))
      this.prefData = new PreferenceData(2);
    else if (paramString1.endsWith("preferences3.dat"))
      this.prefData = new PreferenceData(3);
    else {
      return false;
    }
    return true;
  }

  private void checkData()
  {
    if (this.uidData != null)
    {
      if (this.client == null) {
        localObject = Application.getBot(this);
        this.client = ((Bot)localObject).getClient();
      }
      Object localObject = this.client != null ? this.client.getCurrentUsername() : "";

      if ((!this.uidData.getLastUsed().equals(localObject)) && (this.data != null)) {
        this.uidData.setUID(this.uidData.getLastUsed(), this.data);
        this.data = this.uidData.getUID((String)localObject);
        this.offset = 0;
      } else if (this.data == null) {
        this.data = this.uidData.getUID((String)localObject);
        this.offset = 0;
      }
    }
    else if ((this.prefData != null) && (this.data == null)) {
      this.data = this.prefData.get();
    }
  }

  private void saveData() {
    if ((this.uidData != null) && (this.data != null))
    {
      this.uidData.setUID(this.client != null ? this.client.getCurrentUsername() : "", this.data);
      this.uidData.save();
    }
    else if ((this.prefData != null) && (this.data != null)) {
      this.prefData.set(this.data);
    }
  }

  public void close() throws IOException {
    if (this.raf != null)
      this.raf.close();
  }

  public long length() throws IOException {
    checkData();

    if (this.data != null) {
      return this.data.length;
    }
    return this.raf.length();
  }

  public int read() throws IOException {
    try {
      checkData();

      if (this.data != null) {
        if (this.data.length <= this.offset) {
          return -1;
        }
        return 0xFF & this.data[(this.offset++)];
      }

      return this.raf.read();
    } catch (Exception localException) {
      localException.printStackTrace();
    }

    return -1;
  }

  public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2) throws IOException
  {
    checkData();

    if (this.data != null) {
      try
      {
        if (paramArrayOfByte.length < paramInt1 + paramInt2) {
          paramInt2 = paramArrayOfByte.length - paramInt1;
        }
        if (this.data.length < this.offset + paramInt2) {
          paramInt2 = this.data.length - this.offset;
        }
        if (paramInt2 <= 0) {
          return -1;
        }
        for (int i = 0; i < paramInt2; i++) {
          paramArrayOfByte[(paramInt1 + i)] = this.data[(this.offset++)];
        }
        return paramInt2; } catch (Exception localException) {
        localException.printStackTrace();
      }
    }
    return this.raf.read(paramArrayOfByte, paramInt1, paramInt2);
  }

  public void seek(long paramLong) throws IOException {
    checkData();

    if (paramLong < 0L) {
      throw new IOException("pos < 0");
    }
    if (this.data != null)
      this.offset = (int)paramLong;
    else
      this.raf.seek(paramLong);
  }

  public void write(byte[] paramArrayOfByte, int paramInt1, int paramInt2) throws IOException {
    checkData();

    if (this.data != null)
    {
      if (paramArrayOfByte.length < paramInt1 + paramInt2) {
        paramInt2 = paramArrayOfByte.length - paramInt1;
      }
      if (paramInt2 <= 0) {
        return;
      }

      if (this.data.length < this.offset + paramInt2)
      {
        byte[] arrayOfByte = this.data;
        this.data = new byte[this.offset + paramInt2];
        for (int j = 0; j < (this.offset <= arrayOfByte.length ? this.offset : arrayOfByte.length); j++) {
          this.data[j] = arrayOfByte[j];
        }
      }

      for (int i = 0; i < paramInt2; i++) {
        this.data[(this.offset++)] = paramArrayOfByte[(paramInt1 + i)];
      }
      saveData();
    }
    else {
      this.raf.write(paramArrayOfByte, paramInt1, paramInt2);
    }
  }

  public void write(int paramInt) throws IOException {
    checkData();

    if (this.data != null)
    {
      if (this.data.length < this.offset + 1)
      {
        byte[] arrayOfByte = this.data;
        this.data = new byte[this.offset + 1];
        for (int i = 0; i < (this.offset <= arrayOfByte.length ? this.offset : arrayOfByte.length); i++) {
          this.data[i] = arrayOfByte[i];
        }
      }

      this.data[(this.offset++)] = (byte)paramInt;
      saveData();
    }
    else {
      this.raf.write(paramInt);
    }
  }
}